﻿using System;
using System.Threading.Tasks;

namespace Sidvall.SystemEnvironment.Uwp
{
    public class Launcher : ILauncher
    {
        #region Public Members

        #region LaunchFileAsync

        public async Task<bool> LaunchFileAsync(string filePath, LauncherOptions options)
        {
            Windows.Storage.StorageFile file;

            if (filePath.StartsWith("?"))
                file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(filePath.Substring(1));
            else
                file = await Windows.Storage.StorageFile.GetFileFromPathAsync(filePath);
            if (options != null)
                return await Windows.System.Launcher.LaunchFileAsync(file, GetLauncherOptions(options));
            return await Windows.System.Launcher.LaunchFileAsync(file);
        }

        #endregion
        #region LaunchFolderAsync

        public async Task<bool> LaunchFolderAsync(string folderPath, FolderLauncherOptions options)
        {
            Windows.Storage.StorageFolder folder;

            if (folderPath.StartsWith("?"))
                folder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync(folderPath.Substring(1));
            else
                folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(folderPath);
            if (options != null)
                return await Windows.System.Launcher.LaunchFolderAsync(folder, GetLauncherOptions(options));
            return await Windows.System.Launcher.LaunchFolderAsync(folder);
        }

        #endregion
        #region LaunchUriAsync

        public async Task<bool> LaunchUriAsync(Uri uri, LauncherOptions options)
        {
            if (options != null)
                return await Windows.System.Launcher.LaunchUriAsync(uri, GetLauncherOptions(options));
            return await Windows.System.Launcher.LaunchUriAsync(uri);
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "Launcher - Windows.System.Launcher";
        }

        #endregion

        #endregion
        #region Private Members

        #region GetLauncherOptions

        private static Windows.System.LauncherOptions GetLauncherOptions(LauncherOptions options)
        {
            return new Windows.System.LauncherOptions()
            {
                DesiredRemainingView = GetViewSizePreference(options.DesiredRemainingView),
            };
        }
        private static Windows.System.FolderLauncherOptions GetLauncherOptions(FolderLauncherOptions options)
        {
            return new Windows.System.FolderLauncherOptions()
            {
                DesiredRemainingView = GetViewSizePreference(options.DesiredRemainingView),
            };
        }

        #endregion
        #region GetViewSizePreference

        private static Windows.UI.ViewManagement.ViewSizePreference GetViewSizePreference(ViewSizePreference style)
        {
            switch (style)
            {
                case ViewSizePreference.Default:
                    return Windows.UI.ViewManagement.ViewSizePreference.Default;
                case ViewSizePreference.UseLess:
                    return Windows.UI.ViewManagement.ViewSizePreference.UseLess;
                case ViewSizePreference.UseHalf:
                    return Windows.UI.ViewManagement.ViewSizePreference.UseHalf;
                case ViewSizePreference.UseMore:
                    return Windows.UI.ViewManagement.ViewSizePreference.UseMore;
                case ViewSizePreference.UseMinimum:
                    return Windows.UI.ViewManagement.ViewSizePreference.UseMinimum;
                case ViewSizePreference.UseNone:
                    return Windows.UI.ViewManagement.ViewSizePreference.UseNone;
                default:
                    throw new System.ArgumentException("Invalid style");
            }
        }

        #endregion

        #endregion
    }
}
